//
//  HMOAuthController.swift
//  HMSlender
//
//  Created by 赵少朋赵少朋 on 17/1/18.
//  Copyright © 2017年 ITheima. All rights reserved.
//

import UIKit
import YYModel

class HMOAuthController: UIViewController {

    // MARK: - loadView方法
    override func loadView() {
        
        view = webView
        let urlStr = "https://api.weibo.com/oauth2/authorize?client_id=\(APPKEY)&redirect_uri=\(REDIRECT_URI)"
        let url = URL(string: urlStr)
        if let u = url {
            
            let request = URLRequest(url: u)
            webView.loadRequest(request)
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        title = "微博登陆"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: nil, title: "取消", target: self, action: #selector(cancelItemClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(imgName: nil, title: "自动填充", target: self, action: #selector(automatismFillClick))
    }

    
    //取消按钮
    
    func cancelItemClick() -> () {
        
        dismiss(animated: true, completion: nil)
    }
    
    //自动填充
    func automatismFillClick() ->() {
        //js注入语句
        let jsString = "document.getElementById('userId').value='\(HMWEIBONAME)';document.getElementById('passwd').value='\(HMWBPASSWD)';"
        webView.stringByEvaluatingJavaScript(from: jsString)
    }
    
    // MARK: - 懒加载控件
    private lazy var webView: UIWebView = {
    
        let webVw = UIWebView()
        webVw.delegate = self
        return webVw
    }()
    
    
}


extension HMOAuthController:UIWebViewDelegate {
    
    func webViewDidStartLoad(_ webView: UIWebView) {
        
        
    }
    func webViewDidFinishLoad(_ webView: UIWebView) {
        
    }
    
    // webView 将要加载的请求 会走该方法 默认返回true 允许加载将要加载的请求
    // 默认如果不实现该代理方法 默认都是允许加载的

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        let requestStr = request.url?.absoluteString
        
        if let u = requestStr, u.hasPrefix(REDIRECT_URI) {
            
            //请求参数部分
            let query = request.url?.query
            if let q = query {
            
                let code = q.substring(from: "code=".endIndex)
//                print("ok 搞定",code)
                
                HMOAuthModel.shared.getUserAccount(code: code, isFinish: { (isFinish) in
                    
                    if isFinish {
                        //获取数据成功
                        self.dismiss(animated: false, completion: { 
                            
                            NotificationCenter.default.post(name: NSNotification.Name(rawValue: SETROOTVIEW), object: "hehe", userInfo: nil)
                            
                        })
                    } else {
                    
                        //失败
                        return
                    }
                })
            }
        }
        
        return true
    }
    
    
    
    
}

